package cn.szzsi._web;

import cn.szzsi.common.bean.dto.Msg;
import cn.szzsi.common.kit.CommonUtil;
import cn.szzsi.common.model.Member;
import cn.szzsi.common.model.SiteConfig;
import cn.szzsi.common.model.UserFeedback;
import cn.szzsi.common.model.UserFeedbackReply;
import cn.szzsi.service.UserFeedBackService;
import com.jfinal.aop.Clear;
import com.jfinal.kit.JsonKit;

import java.util.*;


public class UserFeedBackController extends BaseWebController<UserFeedback>{
    private String path;


    //返回界面
    @Clear
    public void record(){
        String shopId=getPara("shopId");
        path= CommonUtil.getPathByUidAndType(shopId,CommonUtil.isMobileDevice(getRequest()));//获取模板路径

        //业务功能
        List<UserFeedback> list = UserFeedBackService.me.getAllUserFeedBack(getLoginMember());
        setAttr("list",list);

        render(CommonUtil.getRealTemplatePath(path,"feedback_list.html"));
    }

    //返回数据
    public void recordList(){
        Member loginMember=getLoginMember();
        List<UserFeedback> userFeedBacks= UserFeedback.dao.find("select * from mall_user_feedback where mid=?",loginMember.getId());
        List<Object> list=new ArrayList<Object>();
        if(userFeedBacks.size()>0){
            for(UserFeedback userFeedBack:userFeedBacks){
                Map<String,Object> map=new HashMap();
                Integer feedbackId=userFeedBack.getInt("id");
                String content=userFeedBack.getStr("content");//反馈内容
                map.put("content",content);
                UserFeedbackReply userFeedBackReply= UserFeedbackReply.dao.getByFeedbackId(feedbackId);
                if(userFeedBackReply!=null){
                    String rcontent= userFeedBackReply.getStr("content");
                    Date rDate=userFeedBackReply.getDate("createDate");
                    map.put("rcontent",rcontent);
                    map.put("rDate",rDate);
                }else{
                    map.put("rcontent","");
                    map.put("rDate","");
                }
                list.add(map);
            }
        }
        renderJson(Msg.success(list));
    }

    @Clear
    public void add(){
        String shopId=getPara("shopId");
        SiteConfig siteConfig= SiteConfig.getByShopId(shopId);
        path= CommonUtil.getPathByUidAndType(shopId,CommonUtil.isMobileDevice(getRequest()));//获取模板路径
        setAttr("shopId",shopId);
        setAttr("siteConfig",siteConfig);
        setAttr("path",path);
        setAttr("base", getRequest().getContextPath());
        setAttr("loginMember",getLoginMember());
        render(CommonUtil.getRealTemplatePath(path,"user_feedback_input.html"));
    }

    @Clear
    public void save(){
        String uid=getPara("shopId");
        String content=getPara("content");
        String name=getPara("name");
        String phone=getPara("phone");
        UserFeedback.dao.saveInfo(content,name,phone,uid,getLoginMember());
        renderJson(Msg.SUCCESS);;

    }
}
